package com.tyijian.admin.repository.jpa;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.tyijian.admin.entity.SysRole;
import com.tyijian.admin.entity.SysUser;

public interface UserDao extends PagingAndSortingRepository<SysUser, Long>, JpaSpecificationExecutor<SysUser> {
	/**
	 * 根据用户名查找用户
	 * 
	 * @param username
	 * @return
	 */
	SysUser findByLoginname(String loginname);

	/**
	 * 根据用户ID查询roleId的数组
	 * 
	 * @param userid
	 * @return
	 */
	@Modifying
	@Query(value = "select su.role_id from sys_user_role su where su.user_id=?1 ", nativeQuery = true)
	Long[] findSysUserRoleIds(Long userid);

	/**
	 * 根据用户ID查找用户的角色列表
	 * 
	 * @param userid
	 * @return
	 */
	@Modifying
	@Query(value = "select s from SysRole s where s.id in :ids")
	List<SysRole> getRoleListByUserId(@Param("ids") Long[] ids);

	/**
	 * 保存用户角色
	 * 
	 * @param userid
	 * @param roleid
	 * @return
	 */
	@Modifying
	@Query(value = "insert into sys_user_role (user_id, role_id) VALUES (?1, ?2)", nativeQuery = true)
	void saveUserRole(Long userid, Long roleid);

	
	/**
	 * 删除用户角色
	 * 
	 * @param userid
	 * @param roleid
	 * @return
	 */
	@Modifying
	@Query(value = "delete from sys_user_role where user_id = ?1 ", nativeQuery = true)
	void deleteUserRole(Long userid);
}